package com.neusoft.neu.his.myhis.tv;

import com.neusoft.neu.his.myhis.entity.Student;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
    import javafx.util.Callback;

/**
 * Sample Sample
 *
 * @author Administrator
 * @version 2024/06/07 16:42
 **/
public class Sample {
    @FXML
    private Button mBtnDelete;

    @FXML
    private TableView mTable;

    @FXML
    private TextField mTxtName;

    @FXML
    private TextField mTxtSex;

    @FXML
    private TableColumn<Student, Boolean> mColumnSelect;

    @FXML
    private TableColumn<Student, String> mColumnName;

    @FXML
    private TableColumn<Student, String> mColumnSex;

    private final ObservableList<Student> data =
            FXCollections.observableArrayList();

    @FXML
    public void initialize() {
        mColumnSelect.setCellValueFactory(new PropertyValueFactory<Student, Boolean>("selected"));

        mColumnName.setCellValueFactory(new PropertyValueFactory<>("name"));
        mColumnSex.setCellValueFactory(new PropertyValueFactory<>("sex"));


        data.add(new Student("张三", "男"));

        data.add(new Student("李四", "女"));

        data.add(new Student("王五", "男"));



        mTable.setItems(data);
        mTable.setEditable(true);

        mColumnSelect.setCellFactory(
                CellFactory.tableCheckBoxColumn(new Callback<Integer, ObservableValue<Boolean>>() {
                    @Override
                    public ObservableValue<Boolean> call(Integer index) {
                        final Student g = (Student) mTable.getItems().get(index);
                        ObservableValue<Boolean> ret =
                                new SimpleBooleanProperty(g, "selected", g.getSelected());
                        ret.addListener(new ChangeListener<Boolean>() {
                            @Override
                            public void changed(
                                    ObservableValue<? extends Boolean> observable,
                                    Boolean oldValue, Boolean newValue) {
                                g.setSelected(newValue);
                            }
                        });
                        return ret;
                    }
                }));
    }

    @FXML
    public  void onDeleteAction(){
        System.out.println(123);
        deleteStudents();
    }
    private boolean deleteStudents() {
        int size = data.size();
        if (size <= 0) {
            return false;
        }
        for (int i = size - 1; i >= 0; i--) {
            Student s = data.get(i);
            if (s.getSelected()) {
                data.remove(s);
            }
        }
        return true;
    }


    @FXML
    public  void  onAdd(){
        if (mTxtName.getText() != null && mTxtSex.getText() != null) {
            data.add(new Student(
                    mTxtName.getText(),
                    mTxtSex.getText()));
            mTxtName.clear();
            mTxtSex.clear();
        }

    }
}